﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using BitPortal.Business;

namespace BitPortal.Admin.Config.BaseConfig
{
    public partial class Default : Controls.BaseAdminPage
    {
        protected override void OnInit(EventArgs e)
        {
            PermissionBLL.CheckRoleThrowException(role: "RootAdmin", useExceptionHandler: true);
            base.OnInit(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                BindData();
            }
        }

        protected void BindData()
        {
            this.Store1.DataSource = BitPortal.Business.PortalConfigBLL.GetAll();
            this.Store1.DataBind();
        }
        protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            BindData();
        }

        protected void btnAdd_Click(object sender, DirectEventArgs e)
        {
            ConfigAddEdit1.BindData(null);
        }

        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
            if (sm.SelectedRows.Count > 0)
            {
                ConfigAddEdit1.BindData(sm.SelectedRows[0].RecordID);
            }
        }

        protected void btnDelete_Click(object sender, DirectEventArgs e)
        {
            int record = 0;
            RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                if (BitPortal.Business.PortalConfigBLL.Delete(row.RecordID) > 0)
                {
                    record++;
                }
            }
            Notification.Show(new NotificationConfig
            {
                Title = "Thông báo",
                Icon = Icon.Information,
                Html = "Xóa " + record + " cấu hình thành công!"
            });
            sm.SelectedRows.Clear();
        }
    }
}